#  python3.11.3  pip install PyQt5 CS社 2023/10/18 copyright
import sys
import random
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QDial, QPushButton

class PointNameApp(QWidget):
   def __init__(self):
       super().__init__()

       self.init_ui()

   def init_ui(self):
       self.setWindowTitle('教学小助手1.5——点名器')

       layout = QVBoxLayout()
       
       self.label1 = QLabel('转动设置起始学号', self)
       self.label1.setStyleSheet("font-size:15px ;color: purple;")
       layout.addWidget(self.label1)

       #旋钮1
       self.dial1 = QDial(self)
       self.dial1.setRange(0, 60)
       self.dial1.setValue(1)
       self.dial1.setNotchesVisible(True)
       self.dial1.valueChanged.connect(self.update_label)
       layout.addWidget(self.dial1)

       self.label2 = QLabel('转动设置终止学号', self)
       self.label2.setStyleSheet("font-size: 15px; color:purple;")
       layout.addWidget(self.label2)
      
       #旋钮2
       self.dial2 = QDial(self)
       self.dial2.setRange(0, 60)
       self.dial2.setValue(60)
       self.dial2.setNotchesVisible(True)
       self.dial2.valueChanged.connect(self.update_label)
       layout.addWidget(self.dial2)

       self.start_button = QPushButton('开始', self)
       self.start_button.clicked.connect(self.start_pointname)
       layout.addWidget(self.start_button)

       self.end_button = QPushButton('结束', self)
       self.end_button.clicked.connect(self.end_pointname)
       layout.addWidget(self.end_button)

       self.label3 = QLabel('选出的学号：', self)
       self.label3.setStyleSheet("font-size: 20px; font-weight: bold; color:red;")
       layout.addWidget(self.label3)
 

       self.setLayout(layout)

       self.setGeometry(800, 400, 400, 200)
       self.show()

   def update_label(self):
       self.label1.setText(f'起始学号：{self.dial1.value()}')
       self.label2.setText(f'终止学号：{self.dial2.value()}')
   def start_pointname(self):
       self.start_button.setEnabled(False)
       self.end_button.setEnabled(True)

       self.timer = QTimer()
       self.timer.timeout.connect(self.generate_random_number)
       self.timer.start(100)

   def generate_random_number(self):
       if self.dial1.value()<self.dial2.value():
          self.random_number = random.randint(self.dial1.value(), self.dial2.value())
          self.label3.setText('选出的学号：'+str(self.random_number))
       else:
           self.label3.setText('起始学号大于终止学号了。重新选择数据')
       

   def end_pointname(self):
       self.start_button.setEnabled(True)
       self.end_button.setEnabled(False)
       self.timer.stop()
       self.timer.deleteLater()

if __name__ == '__main__':
   app = QApplication(sys.argv)
   point_name_app = PointNameApp()
   sys.exit(app.exec_())